package cn.wowjoy.pda.android_pharmacy.mvvm.presenter.staticH;

import android.content.Intent;
import android.util.Log;

import java.io.Serializable;
import java.util.List;

import cn.wowjoy.pda.android_pharmacy.api.ApiService;
import cn.wowjoy.pda.android_pharmacy.common.core.BasePresenter;
import cn.wowjoy.pda.android_pharmacy.common.core.http.ServiceFactory;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.EditUtils;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MDialog;
import cn.wowjoy.pda.android_pharmacy.common.widget.MyToast;
import cn.wowjoy.pda.android_pharmacy.model.xml.XmlUtils;
import cn.wowjoy.pda.android_pharmacy.model.xmlBaseResponse.RoleInfoResponseEnvelope;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.inHospitalPatientInfo.InHospitalPatientBeen;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.pendingTextInfo.PendingTextNode;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.quey.QueryBeen;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.quey.QueryNode;
import cn.wowjoy.pda.android_pharmacy.model.xmlEntity.staticPatientInfo.StaticPatientNode;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.staticH.StaticCheckIndexViewModel;
import cn.wowjoy.pda.android_pharmacy.ui.activity.staticH.check.StaticCheckMedicineDetailActivity;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/8/16.
 */

public class StaticCheckIndexPresenter extends BasePresenter<StaticCheckIndexViewModel> {
    private ApiService mApiService;
    private MDialog waitDialog;

    @Override
    public void onCreatePresenter() {
        mApiService = (ApiService) ServiceFactory.getInstance().getService(ApiService.class);

    }
    public void quey(String CXDJLX, final String DJJLID){
        //TODO:联网获取数据
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(QueryNode.getContent("PDAQ09",CXDJLX,DJJLID)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }
                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
                        QueryBeen queryBeen = XmlUtils.getQueryBeen(xmlData.body.roleInfoResponse.retXml);
                        if( null != queryBeen){
                            if(Contants.HOSPITAL_STATIC_TAKE.equals(queryBeen.getDJDQZT()) || Contants.HOSPITAL_STATIC_CHECK.equals(queryBeen.getDJDQZT())){
                                loadData(DJJLID);
                                return;
                            }
                            switch (queryBeen.getDJDQZT()){
                                case Contants.FIRST:
                                    CreateDialog.failDialog3(activity,"无法进行审核","此取药单当前状态为未取,请重新确认");
                                    break;
                                case Contants.SEND:
                                    CreateDialog.failDialog3(activity,"无法进行审核","此取药单当前状态为已送出,请重新确认");
                                    break;
                                case Contants.RECEIVER:
                                    CreateDialog.failDialog3(activity,"无法进行审核","此取药单当前状态为已接收,请重新确认");
                                    break;
                                case Contants.ERROR:
                                    CreateDialog.failDialog3(activity,"无法进行审核","此取药单当前状态为异常,请重新确认");
                                    break;
                            }
                        }
                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
    public void loadData(final String DJJLID) {
        //TODO:联网获取数据
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(StaticPatientNode.getContent("PDAQ03", DJJLID)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }
                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
//                        waitDialog.dismiss();
                        //TODO:住院信息获取失败 不跳转并提示重新扫码
                        if (!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)) {
                            new MyToast(activity).showinfo("信息获取失败，请重新扫码");
                            return;
                        }

                        //TODO:住院信息获取正确 跳转并传值
                        List<InHospitalPatientBeen> inHospitalPatientBeenList = XmlUtils.getInHospitalPatientBeenList(xmlData.body.roleInfoResponse.retXml);
                        for(InHospitalPatientBeen inHospitalPatientBeen: inHospitalPatientBeenList ){
                            String trim = EditUtils.cn2py(inHospitalPatientBeen.getZZYPMC());
                            inHospitalPatientBeen.setPingYin(trim.replaceAll(" ", ""));
                        }
                        Intent intent = new Intent(activity, StaticCheckMedicineDetailActivity.class);
                        intent.putExtra("inHospitalPatientBeenList", (Serializable) inHospitalPatientBeenList);
                        intent.putExtra("DJJLID", DJJLID);
                        activity.startActivity(intent);
                    }

                    @Override
                    public void onComplete() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
    /**
     * 1 门诊当日未取药处方数
     2 住院取完药 待审核数量
     * @param CXFSLX
     */
    public void setPendingNum(String CXFSLX){
        Disposable disposable = null;
        disposable = mApiService.doXml(XmlUtils.prepare(PendingTextNode.getContent("PDAQ07",CXFSLX)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableObserver<RoleInfoResponseEnvelope>() {
                    @Override
                    protected void onStart() {
                        if(null == waitDialog){
                            waitDialog = CreateDialog.waitingDialog(activity);
                        }
                    }

                    @Override
                    public void onNext(RoleInfoResponseEnvelope xmlData) {
                        CreateDialog.dismiss(activity);
                        //TODO:获取待完成订单
                        if(!"0".equals(xmlData.body.roleInfoResponse.Etrack_ProcInterfaceResult)){
//                            new MyToast(activity).showinfo("未取药信息获取失败");
                            return;
                        }
                        //TODO:当日未取药处方数 / 住院取完药，待审核的数量     PDAQ07
                        String pendingNum =XmlUtils.getPendingNum(xmlData.body.roleInfoResponse.retXml);
//                          Log.e(TAG, "pendingNum: "+ pendingNum);
                        mOnReceivePendingNum.onPending(pendingNum);
                    }
                    @Override
                    public void onComplete() {
                    }
                    @Override
                    public void onError(Throwable e) {
                        CreateDialog.dismiss(activity);
                        if(null != e && null != e.getMessage()){
                            Log.e("Throwable", e.getMessage());
                            new MyToast(activity).showinfo(e.getMessage());
                        }
                    }
                });
        addDisposable(disposable);
    }
    private OnReceivePendingNum mOnReceivePendingNum;
    public   interface OnReceivePendingNum{
        void onPending(String onPendingNum);
    }
    public void registerOnReceivePendingNum(OnReceivePendingNum onReceivePendingNum){
        this.mOnReceivePendingNum=onReceivePendingNum;
    }

}
